﻿using DAL.Helper;
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class DALORM
    {
        public static int  SaveBySql(object model)
        {
            //获取实体对象的属性数组
            PropertyInfo[] proArray = model.GetType().GetProperties();
            //获取标识列
            string identity = GetIdentity(proArray);
            //获取所有非对应数据表字段的属性(拓展属性)
            List<string> nonPropertyList = GetNonFieldProperty(proArray);

            //组合sql语句
            StringBuilder sqlField = new StringBuilder("insert into " + model.GetType().Name + "(");
            StringBuilder sqlValues = new StringBuilder(" values (");

            foreach(PropertyInfo item in proArray)
            {
                //过滤标识列和非映射属性
                if (item.Name == identity) continue;
                if (nonPropertyList.Exists(x => x == item.Name)) continue;
                string columnValue = item.GetValue(model, null) + "";//获取属性值
                if (columnValue == null) continue;

                Type fieldType = item.PropertyType;//获取属性的类型
                if(fieldType == typeof(DateTime))
                {
                    DateTime dt;
                    DateTime.TryParse(columnValue, out dt);
                    if (dt <= SqlDateTime.MinValue) continue;//如果时间是最小值
                }

                sqlField.Append(item.Name + ",");//在sql语句中添加一个字段名
                if(fieldType==typeof(string) || fieldType == typeof(DateTime))
                {
                    sqlValues.Append("'" + columnValue + "',");//在sql语句中添加values的值
                }
                else
                {
                    sqlValues.Append(columnValue + ",");
                }
            }
            //整合字段部分
            string start = sqlField.ToString();
            start = start.Substring(0, start.Length - 1) + ")";
            //整合值部分
            string end = sqlValues.ToString();
            end = end.Substring(0, end.Length - 1) + ")";
            //完整的sql语句
            string sqlText = start + end;
            //调用数据访问类
            //return SqlHelper.ExecuteScalar()
            return SqlEasy.ExecuteNonQuery(sqlText);
        }
        /// <summary>
        /// 获取标识列属性
        /// </summary>
        /// <param name="proinfo"></param>
        /// <returns></returns>
        private static string GetIdentity(PropertyInfo[] proinfo)
        {
            string identiy = string.Empty;
            foreach(PropertyInfo item in proinfo)
            {
                object[] attrs = item.GetCustomAttributes(false);
                foreach(var id in attrs)
                {
                    if(id.GetType().Name == "IdentityAttribute")
                    {
                        identiy = item.Name;
                        break;
                    }
                }
            }
            return identiy;
        }
        private static List<string> GetNonFieldProperty(PropertyInfo[] proinfo)
        {
            List<string> list = new List<string>();
            foreach(PropertyInfo item in proinfo)
            {
                object[] attrs = item.GetCustomAttributes(false);
                foreach(var id in attrs)
                {
                    if(id.GetType().Name == "NonTableAttribute")
                    {
                        list.Add(item.Name);
                        break;
                    }
                }
            }
            return list;
        }

    }
}
